package nc.nc_xiuxian;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static nc.nc_xiuxian.NC_XIUXIAN.plugin;
import static nc.nc_xiuxian.NC_XIUXIAN.ywxs;

//属性加载

public class SHUXING {
    public static void SHUXINGLOAD(String playername) throws IOException {
        File file0 = new File(plugin.getDataFolder(),"sx");
        if(!file0.exists()){
            file0.mkdirs();
        }
        File file = new File(file0,playername+".yml");
        YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
        if(!file.exists()){
            file.createNewFile();
            yml.set("gj",ywxs(Math.random() * 3.0));//攻击
            yml.set("fy",ywxs(Math.random() * 3.0));//防御
            yml.set("fs",ywxs(Math.random() * 3.0));//法伤
            yml.set("sm",ywxs(Math.random() * 3.0));//生命值
            yml.set("xy",ywxs(Math.random() * 3.0));//幸运
            yml.set("sb",ywxs(Math.random() * 3.0));//闪避
            yml.set("gf",1.0F);//功法
            yml.set("maxmp",10.0F);//最大蓝条
            yml.set("mp",10.0F);//蓝条
            yml.set("maxjs",10.0F);//最大精神值
            yml.set("js",10.0F);//精神值
            yml.set("maxjq",1000.0F);//最大精力值
            yml.set("jq",1000.0F);//精力值
            yml.set("xw",0.0F);//修为值
            yml.set("jj",0.0F);//境界
            yml.set("xjj",0.0F);//小境界
            yml.save(file);
            LINGGENG.LGLOAD(playername);
        }
        HashMap<String,Double>sx=new HashMap<>();
        for(String s: yml.getKeys(false)){
            sx.put(s,yml.getDouble(s));
        }
        plugin.playersx.put(playername,sx); //属性读取到服务端
    }
    public static double sxloadapi(Player player,String sx){  //方便访问属性值使用的小东西
        HashMap<String,Double>psx=plugin.playersx.get(player.getName());
        switch (sx){
            case "gj":
            case "fy":
            case "fs":
            case "sm":
            case "xy":
            case "sb":
            case "maxmp":
            case "mp":
            case "maxjs":
            case "js":
            case "maxjq":
            case "jq":
            case "xw":
            case "jj":
            case "xjj":
            case "gf":
                return psx.get(sx);
            default:
                return 0.0F;
        }
    }
    public static void sxsetadd(Player player,String s,Double d) throws IOException { //属性增加使用
        HashMap<String,Double>psx=plugin.playersx.get(player.getName());
        double oldsx = psx.get(s);
        double newsx = oldsx+d;
        double gf = psx.get("gf");
        HashMap<String, Integer> lg= LINGGENG.LGTFGET(player);
        int lgs = lg.get("lgs");
        int lgtf = lg.get("lgtf");
        if(s.equals("xw")){
            double sxz = ywxs(d/(lgs*16*lgtf/gf));
            newsx = ywxs(oldsx+sxz);
            if(newsx>=100.0){
                SHENGJISHUXING.sjts(player);
                double xjj = psx.get("xjj");
                Double jj = psx.get("jj");
                if(jj>=1){
                    if(xjj>=4){
                        xjj=0;
                        jj++;
                        psx.put("jj",jj);
                        psx.put("xjj",xjj);
                    }
                }
                else{
                    if(xjj>=10){
                        xjj=0;
                        jj++;
                        psx.put("jj",jj);
                        psx.put("xjj",xjj);
                    }
                }
                newsx=0.0;
                JINGJIE.jjupdata(player,jj);
            }
        }
        psx.put(s,ywxs(newsx));
        plugin.playersx.put(player.getName(),psx);
        sxupdata(player.getName());
        //player.sendMessage("属性:"+s+"\noldsx:"+oldsx+"\nnewsx:"+newsx+"\n增值:"+d);
    }
    public static void sxupdata(String playername) throws IOException { //属性更新使用
        File file0 = new File(plugin.getDataFolder(),"sx");
        if(!file0.exists()){
            file0.mkdirs();
        }
        File file = new File(file0,playername+".yml");
        YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
        if(!file.exists()){
            return;
        }
        HashMap<String,Double>sx=plugin.playersx.get(playername);
        for(String s: sx.keySet()){
            yml.set(s,sx.get(s));
        }
        yml.save(file);
        SHUXINGLOAD(playername);
    }
    public static void playerallsxupdata(Player player){
        double gj = 1.0F;
        double gs = 4.0F;
        double kj = 0.0F;
        double rx = 0.0F;
        double ys = 0.1F;
        double sm = 0.0F;
        ItemStack mainhand = player.getInventory().getItemInMainHand();
        if(mainhand!=null&&mainhand.getType()!= Material.AIR){
            ItemMeta meta = mainhand.getItemMeta();
            if(meta.hasLore()){
                List<String> lore = meta.getLore();
                for(String l:lore){
                    Pattern pattern = Pattern.compile("§[0-9a-fk-or]");
                    Matcher matcher = pattern.matcher(l);
                    l= matcher.replaceAll("");
                    if(l.contains("攻击伤害")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            int value = Integer.parseInt(matcher.group());
                            gj+=value;
                        }
                    }
                    if(l.contains("攻击速度")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            float value = Float.parseFloat(matcher.group())/100;
                            gs+=value;
                        }
                    }
                    if(l.contains("盔甲值")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            int value = Integer.parseInt(matcher.group());
                            kj+=value;
                        }
                    }
                    if(l.contains("盔甲韧性")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            float value = Float.parseFloat(matcher.group())/100;
                            rx+=value;
                        }
                    }
                    if(l.contains("移动速度")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            float value = Float.parseFloat(matcher.group())/1000;
                            ys+=value;
                        }
                    }
                    if(l.contains("最大生命")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            int value = Integer.parseInt(matcher.group());
                            sm+=value;
                        }
                    }
                }
            }
        }
        ItemStack offhand = player.getInventory().getItemInOffHand();
        if(offhand!=null&&offhand.getType()!=Material.AIR){
            ItemMeta meta = offhand.getItemMeta();
            if(meta.hasLore()){
                List<String> lore = meta.getLore();
                for(String l:lore){
                    Pattern pattern = Pattern.compile("§[0-9a-fk-or]");
                    Matcher matcher = pattern.matcher(l);
                    l= matcher.replaceAll("");
                    if(l.contains("攻击伤害")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            int value = Integer.parseInt(matcher.group());
                            gj+=value;
                        }
                    }
                    if(l.contains("攻击速度")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            float value = Float.parseFloat(matcher.group())/100;
                            gs+=value;
                        }
                    }
                    if(l.contains("盔甲值")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            int value = Integer.parseInt(matcher.group());
                            kj+=value;
                        }
                    }
                    if(l.contains("盔甲韧性")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            float value = Float.parseFloat(matcher.group())/100;
                            rx+=value;
                        }
                    }
                    if(l.contains("移动速度")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            float value = Float.parseFloat(matcher.group())/1000;
                            ys+=value;
                        }
                    }
                    if(l.contains("最大生命")){
                        pattern = Pattern.compile("\\d+");
                        matcher = pattern.matcher(l);
                        if (matcher.find()) {
                            int value = Integer.parseInt(matcher.group());
                            sm+=value;
                        }
                    }
                }
            }
        }
        for (ItemStack armorItem : player.getInventory().getArmorContents()) {
            if (armorItem != null&&armorItem.getType()!= Material.AIR) {
                ItemMeta meta = armorItem.getItemMeta();
                if(meta.hasLore()){
                    List<String> lore = meta.getLore();
                    for(String l:lore){
                        Pattern pattern = Pattern.compile("§[0-9a-fk-or]");
                        Matcher matcher = pattern.matcher(l);
                        l= matcher.replaceAll("");
                        if(l.contains("攻击伤害")){
                            pattern = Pattern.compile("\\d+");
                            matcher = pattern.matcher(l);
                            if (matcher.find()) {
                                int value = Integer.parseInt(matcher.group());
                                gj+=value;
                            }
                        }
                        if(l.contains("攻击速度")){
                            pattern = Pattern.compile("\\d+");
                            matcher = pattern.matcher(l);
                            if (matcher.find()) {
                                float value = Float.parseFloat(matcher.group())/100;
                                gs+=value;
                            }
                        }
                        if(l.contains("盔甲值")){
                            pattern = Pattern.compile("\\d+");
                            matcher = pattern.matcher(l);
                            if (matcher.find()) {
                                int value = Integer.parseInt(matcher.group());
                                kj+=value;
                            }
                        }
                        if(l.contains("盔甲韧性")){
                            pattern = Pattern.compile("\\d+");
                            matcher = pattern.matcher(l);
                            if (matcher.find()) {
                                float value = Float.parseFloat(matcher.group())/100;
                                rx+=value;
                            }
                        }
                        if(l.contains("移动速度")){
                            pattern = Pattern.compile("\\d+");
                            matcher = pattern.matcher(l);
                            if (matcher.find()) {
                                float value = Float.parseFloat(matcher.group())/1000;
                                ys+=value;
                            }
                        }
                        if(l.contains("最大生命")){
                            pattern = Pattern.compile("\\d+");
                            matcher = pattern.matcher(l);
                            if (matcher.find()) {
                                int value = Integer.parseInt(matcher.group());
                                sm+=value;
                            }
                        }
                    }
                }
            }
        }
        HashMap<String,Double>playersx=plugin.playersx.get(player.getName());
        double smz = playersx.get("sm");
        double gjl = playersx.get("gj");
        double fy = playersx.get("fy");
        double jq = playersx.get("jq");
        gj+=gjl;
        sm+=smz;
        kj+=fy;
        if(jq<800&&jq>500){
            ys=0.08;
            gs=2;
        }
        if(jq<500&&jq>0){
            ys=0.04;
            gs=1;
        }
        if(jq<=0){
            ys=0.01;
            gs=0;
        }
        Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(20+sm);
        Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_ARMOR)).setBaseValue(kj);
        Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_ARMOR_TOUGHNESS)).setBaseValue(rx);
        Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED)).setBaseValue(ys);
        Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_ATTACK_SPEED)).setBaseValue(gs);
        Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE)).setBaseValue(gj);
    }
}
